{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch.autograd import Variable"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sample gradient"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**********\n",
      "=====simple gradient=====\n",
      "input data is\n",
      "tensor([ 2.,  3.])\n",
      "compute result is\n",
      "tensor(91.5000)\n",
      "input gradients are\n",
      "tensor([ 15.,  18.])\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Software Installation\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:12: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n",
      "  if sys.path[0] == '':\n"
     ]
    }
   ],
   "source": [
    "a = Variable(torch.FloatTensor([2, 3]), requires_grad=True)\n",
    "b = a + 3\n",
    "c = b * b * 3\n",
    "out = c.mean()\n",
    "out.backward()\n",
    "print('*' * 10)\n",
    "print('=====simple gradient=====')\n",
    "print('input data is')\n",
    "print(a)\n",
    "# print(a.data, type(a.data))\n",
    "print('compute result is')\n",
    "print(out.data[0])\n",
    "print('input gradients are')\n",
    "print(a.grad.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## backward on non-scalar output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**********\n",
      "=====no scalar output=====\n",
      "input data is\n",
      "tensor([[ 2.,  3.]])\n",
      "input gradients are\n",
      "tensor([[ 4.,  6.]])\n"
     ]
    }
   ],
   "source": [
    "m = Variable(torch.FloatTensor([[2, 3]]), requires_grad=True)\n",
    "n = Variable(torch.zeros(1, 2))\n",
    "\n",
    "n[0,0] = m[0,0] ** 2\n",
    "n[0,1] = m[0,1] ** 2\n",
    "\n",
    "n.backward(torch.FloatTensor([[1,1]]))\n",
    "print('*' * 10)\n",
    "print('=====no scalar output=====')\n",
    "print('input data is')\n",
    "print(m)\n",
    "# print(a.data, type(a.data))\n",
    "print('input gradients are')\n",
    "print(m.grad.data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## jacobian"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jacobian matrix is\n",
      "tensor([[ 4.,  2.],\n",
      "        [ 3.,  6.]])\n"
     ]
    }
   ],
   "source": [
    "j = torch.zeros(2, 2)\n",
    "k = Variable(torch.zeros(1, 2))\n",
    "m.grad.data.zero_()\n",
    "k[0, 0] = m[0, 0] ** 2 + m[0, 1] * 3\n",
    "k[0, 1] = m[0, 1] ** 2 + m[0, 0] * 2\n",
    "\n",
    "k.backward(torch.FloatTensor([[1, 0]]), retain_graph=True)\n",
    "j[:, 0] = m.grad.data\n",
    "\n",
    "m.grad.data.zero_()\n",
    "k.backward(torch.FloatTensor([[0, 1]]))\n",
    "j[:, 1] = m.grad.data\n",
    "\n",
    "print('jacobian matrix is')\n",
    "print(j)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## view()\n",
    "- 返回一个有相同数据但大小不同的tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([4, 4])\n",
      "torch.Size([16])\n",
      "torch.Size([2, 8])\n"
     ]
    }
   ],
   "source": [
    "x = torch.randn(4, 4)\n",
    "print(x.size())\n",
    "y = x.view(16)\n",
    "print(y.size())\n",
    "z = x.view(-1, 8)\n",
    "print(z.size())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## compute jacobin martix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([ 2.,  1.])\n",
      "tensor([[ 2.,  1.]])\n"
     ]
    }
   ],
   "source": [
    "x = torch.FloatTensor([2, 1])\n",
    "print(x)\n",
    "x = x.view(1, 2)\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## torch.mm\n",
    "\n",
    "- 对矩阵mat1和mat2进行相乘。 \n",
    "- 如果mat1 是一个n×m 张量，mat2 是一个 m×p 张量，将会输出一个 n×p 张量out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 1.,  2.],\n",
      "        [ 3.,  4.]])\n",
      "tensor([[ 5.,  8.]])\n"
     ]
    }
   ],
   "source": [
    "x = Variable(x, requires_grad=True)\n",
    "y = Variable(torch.FloatTensor([[1, 2], [3, 4]]))\n",
    "z = torch.mm(x, y)\n",
    "print(y)\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "jacobian = torch.zeros(2, 2)\n",
    "# dz1/dx1, dz1/dx2\n",
    "z.backward(torch.FloatTensor([[1, 0]]), retain_graph=True)\n",
    "jacobian[:, 0] = x.grad.data\n",
    "x.grad.data.zero_()\n",
    "\n",
    "# dz2/dx1, dz2/dx2\n",
    "z.backward(torch.FloatTensor([[0, 1]]))\n",
    "jacobian[:, 1] = x.grad.data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "======jacobian=====\n",
      "x\n",
      "tensor([[ 2.,  1.]])\n",
      "y\n",
      "tensor([[ 1.,  2.],\n",
      "        [ 3.,  4.]])\n",
      "compute result\n",
      "tensor([[ 5.,  8.]])\n",
      "jacobin matrix is\n",
      "tensor([[ 1.,  2.],\n",
      "        [ 3.,  4.]])\n"
     ]
    }
   ],
   "source": [
    "print('======jacobian=====')\n",
    "print('x')\n",
    "print(x.data)\n",
    "print('y')\n",
    "print(y.data)\n",
    "print('compute result')\n",
    "print(z.data)\n",
    "print('jacobin matrix is')\n",
    "print(jacobian)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
